﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;

namespace POSPlus.EZBarTender
{
    public abstract class LabelNormalizer
    {
        public DataTable LabelInformation { get; set; }

        public LabelNormalizer()
        {
            LabelInformation = new DataTable();

            LabelInformation.Columns.Add("Description");
            LabelInformation.Columns.Add("Vendor");
            LabelInformation.Columns.Add("CircleFile");
            LabelInformation.Columns.Add("Cert_Code");
            LabelInformation.Columns.Add("AvgDailyMovement");
            LabelInformation.Columns.Add("Pack");
            LabelInformation.Columns.Add("Size");
            LabelInformation.Columns.Add("Unit_NP");
            LabelInformation.Columns.Add("UOM");
            LabelInformation.Columns.Add("RegularUpcCheckbit");
            LabelInformation.Columns.Add("RegularUpcNoCheckbit");
            LabelInformation.Columns.Add("Authorized");
            LabelInformation.Columns.Add("RegularPriceLine1");
            LabelInformation.Columns.Add("RegularPriceLine2");
            LabelInformation.Columns.Add("RegularPriceLine3");
            LabelInformation.Columns.Add("UPC");
            LabelInformation.Columns.Add("LocAisle");
            LabelInformation.Columns.Add("LocBlock");
            LabelInformation.Columns.Add("LocSeq");
            LabelInformation.Columns.Add("PrintDate");
            LabelInformation.Columns.Add("BatchNo");
        }

        public abstract void Normalize(DataTable table);

        public DataRow CreateRow(string[] information)
        {
            DataRow row = LabelInformation.NewRow();
            row.ItemArray = (object[])information.Clone();
            return row;
        }

        public String ConvertMoney(double amount)
        {
            double value = Math.Round(amount, 2);

            if (value < 1)
            {
                value = value * 100;
                return value.ToString() + "\u00A2";
            }
            else
            {
                return String.Format("{0:0.00}", value);
            }
        }

        public String CircleFile(int vendor)
        {
            switch (vendor)
            {
                case 1:
                case 351: return ConfigurationManager.AppSettings["RedCircle"];
                case 3: return ConfigurationManager.AppSettings["YellowCircle"];
                case 56: return ConfigurationManager.AppSettings["BlueCircle"];
                default: return ConfigurationManager.AppSettings["Blank"];                
            }
        }

        public String AvgDailyMovement(double movement)
        {
            return String.Format("{0:#.0}", Math.Round(movement, 1));
        }

        public String Unit_NP(double unit)
        {
            String result = "";

            if (unit < 1)
            {
                result = String.Format("{0:0.000}", Math.Round(unit, 3));
                result = "$" + result;
            }
            else
            {
                result = String.Format("{0:0.00}", Math.Round(unit, 2));
                result = "$" + result;
            }

            return result;
        }

        public String RegularUpcCheckBit(int vendor, int cert_code)
        {
            if (vendor == 1 || vendor == 351)
            {
                return String.Format("{0}", cert_code);
            }
            else
            {
                return "";
            }
        }

        public String RegularUpcNoCheckBit(int vendor, int cert_code)
        {
            if (vendor == 3)
                return String.Format("{0}", cert_code);
            else
                return "";
        }

        public String Authorized(int hosted)
        {
            if (hosted == 1)
                return "";
            else
                return "Unauthorized";
        }

        public String PriceLine1(String spriceMethod, String squantity, String sprice, String sgroupPrice)
        {
            int priceMethod = -1;
            int quantity = -1;
            double price = -1;
            double groupPrice = -1;

            int.TryParse(spriceMethod, out priceMethod);
            int.TryParse(squantity, out quantity);
            double.TryParse(sprice, out price);
            double.TryParse(sgroupPrice, out groupPrice);

            switch (priceMethod)
            {
                case 0: return "";
                case 1: return "";
                case 2: return String.Format("Buy {0} At {1}{2}", quantity - 1, ConvertMoney(price), Environment.NewLine);
                case 3: return String.Format("Buy One At {0}{1}", ConvertMoney(price), Environment.NewLine);
                case 4: return String.Format("{0} Limit {1}{2}", ConvertMoney(groupPrice), quantity, Environment.NewLine);
                default: return "";
            }
        }

        public String PriceLine2(string spriceMethod, string squantity, string sprice, string sgroupPrice)
        {
            int priceMethod = -1;
            int quantity = -1;
            double price = -1;
            double groupPrice = -1;

            int.TryParse(spriceMethod, out priceMethod);
            int.TryParse(squantity, out quantity);
            double.TryParse(sprice, out price);
            double.TryParse(sgroupPrice, out groupPrice);

            switch (priceMethod)
            {
                case 0: return String.Format("{0}", ConvertMoney(price));
                case 1: return String.Format("{0}/{1}", quantity, ConvertMoney(groupPrice));
                case 2:
                case 3:
                case 4:
                default: return "";
            }
        }

        public String PriceLine3(string spriceMethod, string squantity, string sprice, string sgroupPrice)
        {
            int priceMethod = -1;
            int quantity = -1;
            double price = -1;
            double groupPrice = -1;

            int.TryParse(spriceMethod, out priceMethod);
            int.TryParse(squantity, out quantity);
            double.TryParse(sprice, out price);
            double.TryParse(sgroupPrice, out groupPrice);

            switch (priceMethod)
            {
                case 0:
                case 1: return "";
                case 2:
                    if (groupPrice == (price * (quantity - 1)))
                    {
                        return "Get One Free!";
                    }
                    else
                    {
                        return String.Format("Get Next For {0}", ConvertMoney(groupPrice - (price * (quantity - 1))));
                    }
                case 3: return String.Format("Buy Quantity For {0}", ConvertMoney(groupPrice));
                case 4: return String.Format("Additional For {0}", ConvertMoney(price));
                default: return "";                
            }
        }
    }
}
